﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GalaSoft.MvvmLight.Threading;
using System.IO.IsolatedStorage;
using QConnectSDK.Models;
using OpenConnect.WPSample.Model;

namespace OpenConnect.WPSample.ViewModel
{
    public class ViewModelLocator
    {
        public ViewModelLocator()
        {
            Instance = this;

            DispatcherHelper.Initialize();
            CreateAuth();
            CreateMain();
        }

      

        public static ViewModelLocator Instance { get; private set; }

        public const string AppName = "腾讯社区开放平台的.NET版本SDK";

        #region Auth
        private static AuthenticationViewModel _auth;
        /// <summary>
        /// Gets the Main property.
        /// </summary>
        public static AuthenticationViewModel AuthStatic
        {
            get
            {
                if (_auth == null)
                {
                    CreateAuth();
                }

                return _auth;
            }
        }

        /// <summary>
        /// Gets the Main property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public AuthenticationViewModel Auth
        {
            get
            {
                return AuthStatic;
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the Main property.
        /// </summary>
        public static void ClearAuth()
        {
            if (_auth != null)
                _auth.Cleanup();

            _auth = null;
        }

        public static void CreateAuth()
        {
            if (_auth == null)
            {
                AuthResult auth;
                IsolatedStorageSettings.ApplicationSettings.TryGetValue<AuthResult>("auth", out auth);
                _auth = new AuthenticationViewModel(auth);
            }
        }
        #endregion

        #region Main
        private static MainViewModel _main;
        /// <summary>
        /// Gets the Main property.
        /// </summary>
        public static MainViewModel MainStatic
        {
            get
            {
                if (_main == null)
                {
                    CreateMain();
                }

                return _main;
            }
        }

        /// <summary>
        /// Gets the Main property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public MainViewModel Main
        {
            get
            {
                return MainStatic;
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the Main property.
        /// </summary>
        public static void ClearMain()
        {
            if (_main != null)
                _main.Cleanup();

            _main = null;
        }

        /// <summary>
        /// Provides a deterministic way to create the Main property.
        /// </summary>
        public static void CreateMain()
        {
            if (_main == null)
            {
                _main = new MainViewModel(AuthStatic);
            }
        }
        #endregion

        public static void SaveSetting(string name, object value)
        {
            DispatcherHelper.UIDispatcher.BeginInvoke(() =>
            {
                IsolatedStorageSettings.ApplicationSettings[name] = value;
                IsolatedStorageSettings.ApplicationSettings.Save();
            });
        }


        /// <summary>
        /// Cleans up all the resources.
        /// </summary>
        public static void Cleanup()
        {
            ClearMain();
            ClearAuth();
        }
    }
}
